/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.iterator;

import java.util.Enumeration;
import java.util.Iterator;
import ca.forklabs.baselib.util.iterator.IterableIterator;
import ca.forklabs.baselib.util.iterator.Resources;

/**
 * Class {@code EnumerationIterator} is a <em>forward</em>
 * <em>non-alterable</em> and <em>non-appendable</em> adapter over an
 * {@link Enumeration} to make it pass as an {@link Iterator}.
 *
 * @param   <E>   the type of the elements.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.iterator.EnumerationIterator">Daniel Léonard</a>
 * @version $Revision$
 */
public class EnumerationIterator<E> extends IterableIterator<E> {

//---------------------------
// Instance variable
//---------------------------

   /** The adapted enumeration. */
   private Enumeration<E> enumeration;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructs an {@link Iterator} over the specified {@link Enumeration}.
    * @param   enumuration   the adapted enumeration.
    * @exception   NullPointerException   if the parameter is {@code null}.
    */
   public EnumerationIterator(Enumeration<E> enumuration) {
      this.setEnumeration(enumuration);
      }


//---------------------------
// Accessor and mutator
//---------------------------

   /**
    * Changes the adapted {@link Enumeration}.
    * @param   enumuration   the adapted enumeration.
    * @exception   NullPointerException   if the parameter is {@code null}.
    */
   protected void setEnumeration(Enumeration<E> enumuration) {
      if (null == enumuration) {
         String message = this.getCannotIterateErrorMessage();
         throw new NullPointerException(message);
         }
      this.enumeration = enumuration;
      }

   /**
    * Gets the adapted {@link Enumeration}.
    * @return   the adapted enumeration.
    */
   protected Enumeration<E> getEnumeration() {
      return this.enumeration;
      }


//---------------------------
// Implemented method from java.util.Iterator
//---------------------------

   /**
    * Returns {@code true} if the iterator has more elements. The iterator
    * has more elements if the underlying enumeration has more elements.
    * @return   {@code true} if the iterator has more elements,
    *           {@code false} otherwise.
    */
   @Override
   public boolean hasNext() {
      Enumeration<E> enumuration = this.getEnumeration();
      boolean has_more = enumuration.hasMoreElements();
      return has_more;
      }

   /**
    * Gets the next element in the iteration. It also advances to the next
    * element.
    * @return   the next element in the iteration.
    */
   @Override
   public E next() {
      Enumeration<E> enumuration = this.getEnumeration();
      E element = enumuration.nextElement();
      return element;
      }

   /**
    * Unsupported operation.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   public void remove() {
      String message = this.getCannotRemoveErrorMessage();
      throw new UnsupportedOperationException(message);
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message telling that it is impossible to adapt the
    * enumeration.
    * @return   the formatted message.
    */
   protected String getCannotIterateErrorMessage() {
      String key = Resources.ENUMERATION_ITERATOR_CANNOT_ADAPT;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   /**
    * Gets the error message telling that it is impossible to remove an element.
    * @return   the formatted message.
    */
   protected String getCannotRemoveErrorMessage() {
      String key = Resources.ENUMERATION_ITERATOR_REMOVE_NOT_SUPPORTED;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   }
